package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-08-30
 * Time: 16:24
 */
public class ThreadD14 {
    public static void main(String[] args) {
        Object jiangyou =new Object();
        Object cu = new Object();

        //建立两个线程分别表示S和Z两个人
        Thread S = new Thread(() -> {
            synchronized (cu){
                //这里让程序休眠确保S可以顺利拿到cu
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (jiangyou){
                    //这里打印一下看程序是否可以顺利的执行到此处
                    System.out.println("S顺利的拿到了酱油&醋");
                }
            }


        });
        Thread Z = new Thread(() -> {
            synchronized (cu){
                //这里同理让程序休眠，使Z顺利拿到酱油
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (jiangyou){
                    System.out.println("Z顺利的拿到了酱油&醋");

                }
            }
        });
        //让线程运行
        S.start();
        Z.start();
    }
}
